#   This file is part of emesene.
#
#    Eval plugin is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    Eval plugin is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

VERSION = '0.1'
import gtk
import Plugin
import ConversationManager

class MainClass(Plugin.Plugin):
    '''Main plugin class'''
    
    description = _('Censore words on incomming messeges.')
    authors = { 'Guiman' : 'alvarola@gmail.com'}
    website = 'emesene.org'
    displayName = _('Censore')
    name = 'Censore'
    def __init__(self, controller, msn):
        '''Contructor'''
        Plugin.Plugin.__init__(self, controller, msn)
        
        self.description = _('Censore words on incomming messeges.')
        self.authors = { 'Guiman' : 'alvarola@gmail.com'}
        self.website = 'emesene.org'
        self.displayName = _('Censore')
        self.name = 'Censore'
        self.controller = controller
        
        self.config = self.controller.config
        self.config.readPluginConfig(self.name)
        
        self.enabled = False
        
    def start(self):
        '''start the plugin'''
        self.enabled = True
        self.controller.conversationManager.do_receive_message = self.do_receive_message
        #self.connect('receive-message',self.do_receive_message)
        
        
    def do_receive_message(self, conversation, mail, nick, message, format, charset):
        '''Receive a message from'''
        conversation.do_receive_message(mail, nick, self.censoreText(message), format, charset)


    def stop(self):    
        '''stop the plugin'''
        self.enabled = False
        self.controller.conversationManager.do_receive_message = self.controller.conversationManager.do_receive_message
    
    def censoreText(self,message):
        censoredMessage = message.upper()
        if self.enabled :
            for word in self.config.pluginsConfigDict[self.name]:
            	print word
                censoredMessage = censoredMessage.replace(word.upper(),self.config.pluginsConfigDict[self.name][word])
        return censoredMessage.lower();
    
    def check(self):
        return (True, 'Ok')
        
        
        #'''Dialogo de configuracion del complemento de Censura'''
    def configure(self):
        '''display a configuration dialog'''
        l = []
        # name, optionType, label, description, value, options
        l.append( Plugin.Option('censoredWord', str , _('Censored Word'),\
        _('Insert here a word to be censored'), ''))
        l.append( Plugin.Option('censore', str, _('Censore Word'),\
        _('Insert here a censore word'), ''))
        l.append( Plugin.Option('custom', dict,\
        _('To delete , only put the censoredWord and the accept'), _('Defined Commands'),\
        None, self.config.pluginsConfigDict[self.name]))

        #Run up your config
        self.configWindow = Plugin.ConfigWindow(_('Config Plugin'),l)
        response = self.configWindow.run()
        #Get parameters
        if response != None:
            #If parameters are not empty
            print response['censoredWord'].value
            if response['censoredWord'].value != ''\
                and response['censore'].value != '':
                try:
                	if self.config.getPluginValue(self.name,response['censoredWord'].value, '') == response['censore'].value:
                    	#rule exists
            			return (False, _('Failed to add Word: Rule Already Exists'))
                	else:
                		self.config.setPluginValue(self.name, response['censoredWord'].value , response['censore'].value)
                except KeyError:
                    #happens when trying to check if the rule exist
                    #If key dosent exist , should be generated!
                	self.config.setPluginValue(self.name, response['censoredWord'].value , response['censore'].value)
            else:
            	try:
            	    if response['censoredWord'].value != '':
            	        del self.config.pluginsConfigDict[self.name][response['censoredWord'].value]
                except KeyError:
                	return (False, _('Failed to add Word: Censore Word Missing'))
        return True
